package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;
import com.honey.mybatis.generator.entity.EntityConstant;

public class BaseDaoSelectOne2Method extends AbstractMethod {

	private static final FullyQualifiedJavaType RETURN_TYPE = EntityConstant.ENTITY_GENERIC_TYPE; 
	
	private static final String[] JAVA_DOC = new String[]{
		"查询一条记录，如果有多条mybais抛出异常。",
		"注意spring自动关闭数据库连接.",
	} ;
	
	public BaseDaoSelectOne2Method(AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return DaoConstant.SELECT_ONE_METHOD_NAME;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return new Parameter[]{
			new Parameter(DaoConstant.SQL_SESSION_PACKAGE,"session"),
			new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"statement","Mybatis映射配置。"),
			new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),"parameter","查询条件。"),
		};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
//		MethodDecoration answer = super.getMethodDecoration();
//		answer.setVisibility(JavaVisibility.PROTECTED);
//		answer.setFinal(true);
//		return answer;
		
		return super.getDependentModel().getMethodDecoration(null);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		IfBlock ifblock = new IfBlock("parameter == null");
		ifblock.addBodyLine("return null;");
		method.addBlocks(ifblock);
		method.addBodyLine(EntityConstant.ENTITY_GENERIC_NAME+" entity =("+EntityConstant.ENTITY_GENERIC_NAME+")session.selectOne(statement, parameter)");
		method.addBodyLine("return entity");
	}
}
